package data;

import java.util.HashMap;

public class Binary implements Constraint {
	public enum BinaryType {
		Equal,
		NotEqual;
	}
	
	public BinaryType Type;
	public Item VariableA;
	public Item VariableB;
	
	public Binary(BinaryType type, Item variableA, Item variableB) {
		Type = type;
		VariableA = variableA;
		VariableB = variableB;
	}
	
	public boolean IsSatisfied() {
		if (VariableA.Domain.size() != VariableB.Domain.size()) {
			return false;
		}
		
		for (int i = 0; i < VariableA.Domain.size(); i++) {
			if (!VariableB.Domain.contains(VariableA.Domain.get(i))) {
				return Type == BinaryType.NotEqual;
			}
		}
		
		return Type == BinaryType.Equal;
	}

	@Override
	public boolean IsSatisfied(HashMap<Item, Bag> assignments) {
		boolean containsA, containsB;
		
		containsA = assignments.containsKey(VariableA);
		containsB = assignments.containsKey(VariableB);
		
		if (containsA && containsB) {
			if (assignments.get(VariableA).Name.equals(assignments.get(VariableB).Name)) {
				return Type == BinaryType.Equal;
			} else {
				return Type == BinaryType.NotEqual;
			}
		} else if (!containsA && containsB) {
			if (!VariableA.Domain.contains(assignments.get(VariableB))) {
				return Type == BinaryType.NotEqual;
			}
		} else if (containsA && !containsB) {
			if (!VariableB.Domain.contains(assignments.get(VariableA))) {
				return Type == BinaryType.NotEqual;
			}
		} else if (!containsA && !containsB) {
			return IsSatisfied();
		}
		
		return Type == BinaryType.Equal;
	}
}
